import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/index'
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    user_info: uni.getStorageSync('user_info') || {}, // 用户信息
  },
  getters: {},
  mutations: {
    // 设置用户信息
    SET_USER(state, info) {
      state.user_info = info
      uni.setStorageSync('user_info', info)
    },
    // 清除用户信息
    CLEAR_USER(state) {
      state.user_info = {}
      uni.removeStorageSync('user_info')
    },
  },
  actions: {
    // 获取用户信息
    async getUserInfo({ commit }) {
      const res = await api.common.user('userInfo')
      if (res.code === 0) {
        commit('SET_USER', res.data)
      } else {
        commit('CLEAR_USER')
      }
    },
  },
})
export default store
